package root.electronclass.electronclass.home.widget;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import root.electronclass.electronclass.common.base.BasicAdapter;
import root.electronclass.electronclass.common.base.BindingHolder;
import root.electronclass.electronclass.common.util.HeadUtil;
import root.electronclass.electronclass.mvp.GlobalParameter;
import root.electronclass.electronclass.mvp.base.BaseResponse;
import root.electronclass.electronclass.mvp.entity.HomeExcellentInfoH;
import root.electronclass.electronclass.mvp.rest.RestManager;
import root.electronclass.electronclass.R;
import root.electronclass.electronclass.databinding.HomeExcellentInfoBinding;
import root.electronclass.electronclass.databinding.HomeItemExcellentBinding;

import java.util.Collections;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class HomeExcellentView {
    private static final String TAG = "HomeExcellentView";

    private Context context;
    private HomeExcellentInfoBinding binding;
    private String curType;

    public HomeExcellentView(Context context, ViewGroup view) {
        this.binding = HomeExcellentInfoBinding.bind(view);
        this.context = context;
        init();
    }

    private BasicAdapter<HomeExcellentInfoH, HomeItemExcellentBinding> adapter = new BasicAdapter<HomeExcellentInfoH, HomeItemExcellentBinding>() {
        @Override
        public void onBindViewHolder(@NonNull BindingHolder<HomeItemExcellentBinding> holder, @Nullable HomeExcellentInfoH item) {
            holder.getBinding().tvStudentName.setText(item.getName());
            holder.getBinding().tvDate.setText(item.getCreateTime());
            if (TextUtils.isEmpty(item.getFacePhoto())) {
                holder.getBinding().imgHeader.setActualImageResource(HeadUtil.getHeadPic(item.getGender() == 1 ? 3 : 2));
            } else {
                holder.getBinding().imgHeader.setImageURI(item.getFacePhoto());
            }
        }

        @Override
        public void onBindViewHolder(@NonNull BindingHolder<HomeItemExcellentBinding> holder, int position) {
            super.onBindViewHolder(holder, position);
        }
    };

    private void init() {
        binding.rvStudentList.setVisibility(View.GONE);
        binding.tvType.setText("7天");
        binding.tvType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopWindow();
            }
        });
        request("1");
    }

    public void showPopWindow() {

        PopupWindow popupWindow = new PopupWindow( 80, ViewGroup.LayoutParams.WRAP_CONTENT);
        LinearLayout linearLayout = new LinearLayout(context);
        linearLayout.setOrientation(LinearLayout.VERTICAL);

        TextView textView = new TextView(context);
        textView.setText("7天");
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, 15);
        textView.setTextColor(Color.parseColor("#306EFF"));
        textView.setBackgroundResource(R.drawable.bg_home_type_3);
        textView.setPadding(16, 8,16,8);
        linearLayout.addView(textView);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                request("1");
                binding.tvType.setText("7天");
                popupWindow.dismiss();
            }
        });

        textView = new TextView(context);
        textView.setText("1个月");
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, 15);
        textView.setTextColor(Color.parseColor("#306EFF"));
        textView.setBackgroundResource(R.drawable.bg_home_type_3);
        textView.setPadding(16, 8,16,8);
        linearLayout.addView(textView);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                request("2");
                popupWindow.dismiss();
                binding.tvType.setText("1个月");
            }
        });

        textView = new TextView(context);
        textView.setText("1学期");
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, 15);
        textView.setTextColor(Color.parseColor("#306EFF"));
        textView.setBackgroundResource(R.drawable.bg_home_type_3);
        textView.setPadding(16, 8,16,8);
        linearLayout.addView(textView);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                request("3");
                popupWindow.dismiss();
                binding.tvType.setText("1学期");

            }
        });

        popupWindow.setContentView(linearLayout);
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.showAsDropDown(binding.tvType);
    }

    public void refresh() {
        request(curType);
    }

    public void request(String type) {
        curType = type;
        long id = GlobalParameter.getEcardInfo().getClassId();
        long sid = GlobalParameter.getEcardInfo().getSchoolId();
        RestManager.getInstance().initRest().excellentStudent(String.valueOf(id), String.valueOf(sid), type).enqueue(new Callback<BaseResponse<List<HomeExcellentInfoH>>>() {
            @Override
            public void onResponse(Call<BaseResponse<List<HomeExcellentInfoH>>> call, Response<BaseResponse<List<HomeExcellentInfoH>>> response) {
                if (!TextUtils.equals(type, curType)) {
                    return;
                }
                if (response.isSuccessful()) {
                    binding.rvStudentList.setVisibility(View.VISIBLE);
                    LinearLayoutManager manager = new LinearLayoutManager(context, RecyclerView.VERTICAL, false);
                    binding.rvStudentList.setLayoutManager(manager);
                    if (response.body().getRows() != null) {
                        Collections.sort(response.body().getRows(), (o1, o2) -> {
                            // 按数字由高到低排序
                            return Integer.compare(o2.getGender(), o1.getGender());
                        });
                    }
                    adapter.replaceAll(response.body().getRows());
                    binding.rvStudentList.setAdapter(adapter);
                }
            }

            @Override
            public void onFailure(Call<BaseResponse<List<HomeExcellentInfoH>>> call, Throwable t) {

            }
        });
    }


}
